<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="60%">
    <!-- 搜索栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="mb-15px"
      label-width="68px"
    >
      <el-form-item label="查询条件" prop="name" class="mb-20px">
        <el-input v-model="queryParams.name" class="!w-220px" clearable placeholder="请输入条件" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary"> 搜索 </el-button>
        <el-button @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="listTableRef"
      border
      v-loading="loading"
      :data="list"
      @selection-change="selectionClick"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="一级功能模块" prop="name1" />
      <el-table-column align="center" label="二级功能模块" prop="name2" />
      <el-table-column align="center" label="三级级功能模块" prop="name3" />
      <el-table-column align="center" label="表头字段" prop="name" />
    </el-table>

    <!-- <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="一级功能模块" prop="name1">
        <el-input v-model="formData.name1" placeholder="请输入一级功能模块" />
      </el-form-item>
      <el-form-item label="二级功能模块" prop="name2">
        <el-input v-model="formData.name2" placeholder="请输入二级功能模块" />
      </el-form-item>
      <el-form-item label="三级功能模块" prop="name3">
        <el-input v-model="formData.name3" placeholder="请输入三级功能模块" />
      </el-form-item>
      <el-form-item label="表头字段" prop="name">
        <el-input v-model="formData.name" placeholder="请输表头字段" />
      </el-form-item>
    </el-form> -->
    <template #footer>
      <el-button :disabled="loading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { ElTable } from 'element-plus'
import * as MenuApi from '@/api/system/menu'

defineOptions({ name: 'AddedList' })
const message = useMessage() // 消息弹窗
const list = ref<any>([]) // 列表的数据
const loading = ref(false)
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const queryFormRef = ref() // 搜索的表单
const roleId = ref()
interface User {
  name1: string
  name2: string
  name3: string
  name: string
  id: number
  type: number
  permission: number
}
const queryParams = reactive({
  name: ''
})
const listTableRef = ref<InstanceType<typeof ElTable>>()
const selectionData = ref()

/** 打开弹窗 */
const open = async (id) => {
  dialogVisible.value = true
  dialogTitle.value = '新增'
  roleId.value = id
  getList()
}
const selectionClick = (val: User[]) => {
  let result = val.map((item) => {
    if (item.type == 4) {
      return item.id
    }
  })
  selectionData.value = result.toString()
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MenuApi.listPurview(queryParams)
    list.value = data
  } finally {
    loading.value = false
  }
}
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  loading.value = true
  try {
    const data = {
      roleId: roleId.value,
      menuIds: selectionData.value
    }
    await MenuApi.addList(data)
    message.success('新增成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
